package com.api.getaway.config;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

@Component
public class AccessFilter extends ZuulFilter {

	private final Logger logger = Logger.getLogger(AccessFilter.class);

	@Override
	public Object run() {
		RequestContext ctx = RequestContext.getCurrentContext();
		/*
		 * 
		 * HttpServletRequest request = ctx.getRequest(); Object accessToken =
		 * request.getParameter("accessToken"); if (accessToken == null) {
		 * logger.warn("access token is empty"); ctx.setSendZuulResponse(false);
		 * ctx.setResponseStatusCode(401); return null; }
		 */
		logger.warn("This is a pre filter");
		try {
			doSomething();
		} catch (Exception e) {
			ctx.set("error.status_code", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			ctx.set("error.exception", e);
		}

		return null;
	}

	protected void doSomething(){
		throw new RuntimeException("Exist some errors");
	}

	@Override
	public boolean shouldFilter() {
		return true;
	}

	@Override
	public int filterOrder() {
		return 0;
	}

	@Override
	public String filterType() {
		return "pre";
	}

}
